﻿using DG.Tweening;

using UnityEngine;
using UnityEngine.UI;

#if UNITY_EDITOR
using UnityEditor;
#endif

public class UIMoveComponent : UIAnimation
{
#if UNITY_EDITOR
    [FieldLabel("是否使用当前坐标")]
    public PositionState posState;
#endif

    [FieldLabel("是否相对坐标")]
    public bool isLocal;

    [FieldLabel("起始坐标")]
    public Vector2 startValue;

    [FieldLabel("结束坐标")]
    public Vector2 endValue;

    protected override Tweener GetTweener(RectTransform rectTransform)
    {
        if(isLocal)
        {
            rectTransform.anchoredPosition = startValue;
            return DOTween.To(() => startValue, x => rectTransform.anchoredPosition = x,endValue, this.duration);
        }
        else
        {
            rectTransform.position = rectTransform.ScreenPointToWorld(startValue);
            endValue = rectTransform.ScreenPointToWorld(endValue);
            return DOTween.To(() => startValue, x => rectTransform.position = x, endValue, this.duration);
        }
    }

#if UNITY_EDITOR
    protected override void OnDrawGizmos()
    {
        base.OnDrawGizmos();
        if (Application.isPlaying)
            return;
        if (posState == PositionState.相对父对象)
        {
            isLocal = true;
            startValue = GetComponent<RectTransform>().anchoredPosition;
        }
        else if(posState == PositionState.相对于屏幕左下角)
        {
            isLocal = false;
            startValue = (transform as RectTransform).WorldToScreenPoint();
        }
    }

#endif
}
